﻿using Sys.Core.Models.Dto;
using Sys.Core.Models.Queries;
using Sys.Core.Models.VO;
using Sys.Core.Providers;
using MiniFox;
using MiniFox.Components;
using MiniFox.Infrastructure;
using MiniFox.Platform.Applications;
using MiniFox.Platform.Applications.Auth;

namespace Sys.Core.Services
{
    /// <summary>
    /// 
    /// </summary>
    public class RoleService : ApiServiceBase, IRoleService
    {
        public RoleService(SystemPrincipal principal) : base(principal)
        {

        }
        [AutoWired]
        public IRoleProvider Provider { get; set; }

        public RoleVo Item(string roleId)
        {
            return this.Provider.GetRole(roleId);
        }

        public RoleVo Create(RoleDto role)
        {
            role.RoleID = null;
            if (this.Provider.IsUsedRoleName(role.RoleName))
            {
                throw new InvalidOperationException("角色名已存在");
            }
            role.RoleID = SequenceGUID.NewGUID();
            if (!this.Provider.CreateRole(role))
                return null;
            return Item(role.RoleID);
        }

        public RoleVo Update(RoleDto role)
        {
            if (this.Provider.IsUsedRoleName(role.RoleName, role.RoleID))
            {
                throw new InvalidOperationException("角色名已存在");
            }
            if (!this.Provider.UpdateRole(role))
                return null;
            return Item(role.RoleID);
        }

        public string Delete(string roleId)
        {
            if (this.Provider.DeleteRole(roleId))
                return roleId;

            return null;
        }

        public ResultObject<IEnumerable<RoleVo>> Query(PagedQueryObject<RoleQuery> request)
        {
            return this.Provider.Query(request);
        }

        #region role modules
        public bool AddRoleModules(RoleModuleSet roleModuleSet)
        {
            return this.Provider.AddModules(roleModuleSet.RoleID, roleModuleSet.Modules);
        }

        public bool RemoveRoleModules(RoleModuleSet roleModuleSet)
        {
            return this.Provider.RemoveModules(roleModuleSet.RoleID, roleModuleSet.Modules);
        }

        public ResultObject<IEnumerable<ModuleVo>> InclusionModules(string roleId, PagedQueryObject<ModuleQuery> request)
        {
            return this.Provider.InclusionModules(roleId, request);
        }

        public ResultObject<IEnumerable<ModuleVo>> ExclusionModules(string roleId, PagedQueryObject<ModuleQuery> request)
        {
            return this.Provider.ExclusionModules(roleId, request);
        }
        #endregion

        #region role users

        public bool AddRoleUsers(RoleUserSet roleUserSet)
        {
            return this.Provider.AddUsers(roleUserSet.RoleID, roleUserSet.users);
        }

        public bool RemoveRoleUsers(RoleUserSet roleUserSet)
        {
            return this.Provider.RemoveUsers(roleUserSet.RoleID, roleUserSet.users);
        }

        public ResultObject<IEnumerable<UserVo>> InclusionUsers(string roleId, PagedQueryObject<UserQuery> request)
        {
            return this.Provider.InclusionUsers(roleId, request);
        }

        public ResultObject<IEnumerable<UserVo>> ExclusionUsers(string roleId, PagedQueryObject<UserQuery> request)
        {
            return this.Provider.ExclusionUsers(roleId, request);
        }
        #endregion
    }
}